#include "StdAfx.h"
#include "CenterWindow.h"

CCenterWindow::CCenterWindow(void)
{
}

CCenterWindow::~CCenterWindow(void)
{
}

BOOL CCenterWindow::CreateEx(
	int nX, int nY, int cx, int cy,
	HWND hParent,
	HINSTANCE hInst
)
{
	BOOL ret = CBaseWindow::CreateEx(0, _T("MyCenterWindow"), _T(""), 
		WS_CHILD,
		nX, nY, cx, cy, hParent, HMENU(0), hInst);
	return ret;
}

LRESULT CCenterWindow::WndProcExt(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
	switch(uMsg)
	{
	case WM_PAINT:
		{
			PAINTSTRUCT ps;
			BeginPaint(m_hWnd, & ps);
			OnPaint(ps.hdc);
			EndPaint(m_hWnd, & ps);
		}
		return 0;
	case WM_ERASEBKGND:
		return 0;
	}
	return ::DefWindowProc(hwnd, uMsg, wParam, lParam);
}

void CCenterWindow::OnPaint(HDC dc)
{
	RECT rc;
	GetClientRect(m_hWnd, &rc);
	Graphics g(dc);
	Bitmap bmp(rc.right, rc.bottom);
	Graphics gbmp(&bmp);

	int mainleft = 0, maintop = 0;

	Brush *brush = SolidBrush(Color(0x33, 0x33, 0x33)).Clone();
	gbmp.FillRectangle(brush, 0, 0, rc.right, rc.bottom);

	RectF r[4]={
		RectF((REAL)mainleft, (REAL)maintop+10, 10, (REAL)rc.bottom-24-maintop),
		RectF((REAL)rc.right-14, (REAL)maintop+10, 14, (REAL)rc.bottom-24-maintop),
		RectF((REAL)mainleft+10, (REAL)maintop, (REAL)rc.right-24-mainleft, 10),
		RectF((REAL)mainleft+10, (REAL)rc.bottom-14, (REAL)rc.right-24-mainleft, 14)
	};

	UINT id[4]={IDB_MAINBG_L, IDB_MAINBG_R, IDB_MAINBG_T, IDB_MAINBG_B};
	for(int i=0; i<4; i++)
	{
		Bitmap btmp((int)r[i].Width, (int)r[i].Height);
		Graphics gtmp(&btmp);
		delete(brush);
		brush = TextureBrush(&Bitmap(m_hInst, MAKEINTRESOURCE(id[i])), WrapModeTileFlipXY).Clone();
		gtmp.FillRectangle(brush, 0, 0, (int)r[i].Width, (int)r[i].Height);
		gbmp.DrawImage(&btmp, r[i].GetLeft(), r[i].GetTop());
	}

	Bitmap* b[4]={
		new Bitmap(m_hInst, MAKEINTRESOURCE(IDB_MAINBG_LT)),
		new Bitmap(m_hInst, MAKEINTRESOURCE(IDB_MAINBG_RT)),
		new Bitmap(m_hInst, MAKEINTRESOURCE(IDB_MAINBG_LB)),
		new Bitmap(m_hInst, MAKEINTRESOURCE(IDB_MAINBG_RB))
	};
	Point p[4]={
		Point(mainleft, maintop),
		Point(rc.right-b[1]->GetWidth(),maintop),
		Point(mainleft, rc.bottom-b[2]->GetHeight()),
		Point(rc.right-b[3]->GetWidth(), rc.bottom-b[3]->GetHeight())
	};
	for(int i=0; i<4; i++)
	{
		gbmp.DrawImage(b[i], p[i]);
	}
	delete(brush);
	delete(b[0]); 
	delete(b[1]); 
	delete(b[2]); 
	delete(b[3]); 
	g.DrawImage(&bmp, 0, 0);
}